package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.dao.PoetDao;
import com.coding.pojo.Poet;
import com.coding.service.PoetService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther wangchenghao
 * @date 2020/7/26
 **/
@Service
public class PoetImpl  implements PoetService {

    @Autowired
    PoetDao poetDao;
    @Override
    public void getPoetbyid(int poet_id, BaseModel model) throws Exception {
        Poet poet=poetDao.selectByPrimaryKey(poet_id);
        if (poet!=null) {
            model.setSucceed("获取诗人信息成功", poet);
        } else {
            model.setFailure("获取诗人信息失败");
        }
    }

    @Override
    public void getPoetbyname(String name, BaseModel model) throws Exception {
        Poet poet=poetDao.selectByName(name);
        if (poet!=null) {
            model.setSucceed("获取诗人信息成功", poet);
        } else {
            model.setFailure("获取诗人信息失败");
        }
    }

    @Override
    public void getPoetlist(Poet poet, PageQueryModel pageQueryModel, BaseModel model) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Poet> list = poetDao.queryAlllist(poet);
        PageInfo pageInfo = new PageInfo(list);
        list = pageInfo.getList();
        if (list.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                model.setFailure("我可是有底线的，已经没有下一页了");
            } else {
                model.setOther(2, "很抱歉，没有符合条件的学习资源");
            }
            return;
        }
        model.setSucceed("查询成功，符合条件的资源共" + list.size() + "条", list);
    }

    @Override
    public void getVideobytag(String tag, BaseModel model) throws Exception {

    }
}
